<?php

namespace App\Listeners;

use App\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Models\Loginlog;

class LoginListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  Login  $event
     * @return void
     */
    public function handle(Login $event)
    {
        //判断用户是否登录
        if(Auth::check()){
            $user = Auth::user();
            $sessionData = [
                'user' => [
                    'id' => $user->id,
                    'name'=> $user->name,
                    'is_admin'=> 1,
                    'email' => $user->email
                ]
            ];
            session($sessionData);
            //用户登录成功后进行相应的登录日志写入功能
            $arr = ['uid'=>$user->id,'username'=>$user->name,'ip'=>$_SERVER['REMOTE_ADDR']];
            Loginlog::create($arr);
        }
    }
}
